//
//  NumberDNACell.m
//  DaleTou
//
//  Created by anyfish010 on 2018/5/27.
//  Copyright © 2018年 geren. All rights reserved.
//

#import "NumberDNACell.h"
#import "XGConfig.h"
#import "MBProgressHUD+MJ.h"

@implementation NumberDNACell{
    NSMutableArray *mArrBtnTop;
    NSMutableArray *mArrBtnDown;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    mArrBtnTop = [NSMutableArray array];
    mArrBtnDown = [NSMutableArray array];
    
    CGFloat padingX = 15;
    CGFloat padingY = padingX;
    CGFloat width = (ScreenWidth - padingX * 8)/7;
    CGFloat height = width;
    
    for (int i = 0; i < 35; i ++) {
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(padingX + (padingX + width) * (i % 7), padingY + (padingY + height) * (i/7), width, height)];
        btn.layer.borderWidth = 0.5;
        btn.layer.borderColor = UIColorFromRGB(0xf04100).CGColor;
        btn.layer.cornerRadius = width/2;
        btn.layer.masksToBounds = YES;
        [btn setTitle:[NSString stringWithFormat:@"%d",i + 1] forState:UIControlStateNormal];
        [btn setTitleColor:UIColorFromRGB(0x222222) forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor whiteColor];
        
        btn.tag = i + 100;
        [btn addTarget:self action:@selector(clickBtn1:) forControlEvents:UIControlEventTouchUpInside];
        [self.view1 addSubview:btn];
        [mArrBtnTop addObject:btn];
    }
    
    for (int i = 0; i < 12; i ++) {
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(padingX + (padingX + width) * (i % 7), padingY + (padingY + height)*(i/7), width, height)];
        btn.layer.borderWidth = 0.5;
        btn.layer.borderColor = UIColorFromRGB(0x0070d6).CGColor;
        btn.layer.cornerRadius = width/2;
        btn.layer.masksToBounds = YES;
        [btn setTitle:[NSString stringWithFormat:@"%d",i + 1] forState:UIControlStateNormal];
        [btn setTitleColor:UIColorFromRGB(0x0070d6) forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor whiteColor];
        btn.tag = i + 100;
        [btn addTarget:self action:@selector(clickBtn2:) forControlEvents:UIControlEventTouchUpInside];
        [self.view2 addSubview:btn];
        [mArrBtnDown addObject:btn];
    }
    
}


- (void)clickBtn1:(UIButton *)btn{
    
    int count = 0;
    for (NSDictionary *dict in _mArrTop) {
        if ([[dict valueForKey:@"select"] boolValue]) {
            count = count + 1;
        }
    }
    
    NSMutableDictionary *mDict = [self.mArrTop objectAtIndex:btn.tag - 100];
    BOOL select = [[mDict objectForKey:@"select"] boolValue];
    
    if (count < 5) {
        [mDict setValue:@(!select) forKey:@"select"];
    }else if (count == 5){
        [mDict setValue:@(NO) forKey:@"select"];
    }else{
        [mDict setValue:@(NO) forKey:@"select"];
    }
    
    if (count >= 5 && !select) {
//        [MBProgressHUD showMessage:@"最多选5个"];
    }
    
    self.mArrTop = _mArrTop;

}

- (void)clickBtn2:(UIButton *)btn{
    
    int count = 0;
    for (NSDictionary *dict in _mArrDown) {
        if ([[dict valueForKey:@"select"] boolValue]) {
            count = count + 1;
        }
    }
    
    NSMutableDictionary *mDict = [self.mArrDown objectAtIndex:btn.tag - 100];
    BOOL select = [[mDict objectForKey:@"select"] boolValue];
    
    if (count < 2) {
        [mDict setValue:@(!select) forKey:@"select"];
    }else if (count == 2){
        [mDict setValue:@(NO) forKey:@"select"];
    }else{
        [mDict setValue:@(NO) forKey:@"select"];
    }
    
    if (count >= 2 && !select) {
//        [MBProgressHUD showMessage:@"最多选2个"];
    }
    
    self.mArrDown = _mArrDown;
}

- (void)setMArrTop:(NSMutableArray *)mArrTop{
    _mArrTop = mArrTop;
    for (int i = 0; i < mArrBtnTop.count; i ++) {
        UIButton *btn = [mArrBtnTop objectAtIndex:i];
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitleColor:UIColorFromRGB(0xf04100) forState:UIControlStateNormal];
        BOOL select  = [[[mArrTop objectAtIndex:i] objectForKey:@"select"] boolValue];
        if (select) {
            btn.backgroundColor = UIColorFromRGB(0xf04100);
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
    
}

- (void)setMArrDown:(NSMutableArray *)mArrDown{
    _mArrDown = mArrDown;
    for (int i = 0; i < mArrBtnDown.count; i ++) {
        UIButton *btn = [mArrBtnDown objectAtIndex:i];
        [btn setTitleColor:UIColorFromRGB(0x0070d6) forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor whiteColor];

        BOOL select  = [[[mArrDown objectAtIndex:i] objectForKey:@"select"] boolValue];
        if (select) {
            btn.backgroundColor = UIColorFromRGB(0x0070d6);
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
